package com.eazyimage.lab.service;

import com.eazyimage.lab.entity.Dataeachnum;
import com.eazyimage.lab.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class PackageMesService {
    @Autowired
    public static Map<String,Integer> map = new HashMap<String,Integer>();
    @Autowired
    UserMapper us;
    public static Integer getAllFileNumber(File dir){
        Integer fileCount = 0;
        if(dir==null)
            return 0;
        File[] files=dir.listFiles();
        if(files==null)
            return 0;
        return files.length;
    }

    public HashMap<Integer,List<Dataeachnum>> getuserdatasetid(String username)
    {
        HashMap<Integer,List<Dataeachnum>> result = new HashMap<>();
        List<Integer> usid =  us.getuserdatasetid(username);
        for(int id : usid){
            result.put(id,this.Selectnum(Integer.toString(id)));
        }
        return result;
    }

    public List<Dataeachnum> Selectnum(@RequestParam("DatasetId") String datasetid)
    {

        List<Dataeachnum> dataeachnums = new ArrayList<>();
        File file = new File("/easyimage/backend-Flask/static/dataset/"+datasetid);
        String[] packagename = file.list();
        if(packagename!=null)
        for(String itemName : packagename){
            File subfile = new File("/easyimage/backend-Flask/static/dataset/"+datasetid+"/"+itemName);
           // File subfile = new File("F:/Aparttimejob/"+datasetid+"/"+itemName);
            int datasetnum = PackageMesService.getAllFileNumber(subfile);
            Dataeachnum dc = new Dataeachnum(itemName, datasetnum);
            dataeachnums.add(dc);
        }
        return dataeachnums;
    }

}
